package I.O;

//复制文件内容生成代码成功
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class IOproduce {

	public static void main(String[] args) {
		// 写在配置文件里用读取的
		String pack = "Demo";
		String impo = "";
		String className = "produce";
		String zhushi = "";

		String type = ".java";

		String filePath = "C:\\Java\\eclipse\\wzy\\IO\\";
		String mainPath = "src\\Demo\\";
		String name = className + type;

		File f1 = new File(filePath + mainPath);
		f1.mkdirs();
		if (f1.isDirectory()) {
			System.out.println(f1.getAbsolutePath() + "文件夹创建成功");
		}

		StringBuffer java = new StringBuffer();
		// if (pack != "") { //怎么能写成 空格都为false 1.用正则
		java.append("package " + pack + ";\n\n");
		// }
		if (impo != "") {
			java.append("import " + impo + ";\n\n");
		}
		java.append("public class " + className + " {" + "\n\n");
		java.append("\t" + "public static void main(String[] args) {\n\n");
		java.append("\t}\n");
		java.append("}\n");

		FileOutputStream bwf = null;
		// 数组转成字符串，字符串转byte 会不会有问题
		byte[] strbyte = java.toString().getBytes();
		try {
			// 只能创建不存在的文件,不能创建文件夹
			bwf = new FileOutputStream(filePath + mainPath + name);
			bwf.write(strbyte);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				bwf.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		File f = new File(filePath + mainPath + name);
		if (!f.exists()) {
			System.out.println("文件创建失败");
		} else {
			System.out.println(f.getAbsolutePath() + "文件创建成功");
		}
	}
}
